//
//  MKAPITestTaskBase.m
//  MKAPITests
//
//  Created by 苏尚进 on 2022/9/14.
//  Copyright © 2022 moon. All rights reserved.
//

#import "MKAPITestTaskBase.h"

@interface MKAPITestTaskBase()
@property(nonatomic,assign) CGFloat preProgressPercent;
@property(nonatomic,assign) BOOL progressIncreat;
@property(nonatomic,assign) BOOL progressOver50Percent;
@end

@implementation MKAPITestTaskBase

- (XCTestExpectation *)createCancelExpectationsWithTestCase:(XCTestCase *)testCase{
    self.cancelExpectation = [testCase expectationWithDescription:[NSString stringWithFormat:@"%@.cancelExpectation",NSStringFromClass(self.class)]];
    return self.cancelExpectation;
}
- (XCTestExpectation *)createCompletionExpectationsWithTestCase:(XCTestCase *)testCase{
    self.completionExpectation = [testCase expectationWithDescription:[NSString stringWithFormat:@"%@.completionExpectation",NSStringFromClass(self.class)]];
    return self.completionExpectation;
}
- (XCTestExpectation *)createBeginExpectationsWithTestCase:(XCTestCase *)testCase{
    self.beginExpectation = [testCase expectationWithDescription:[NSString stringWithFormat:@"%@.beginExpectation",NSStringFromClass(self.class)]];
    return self.beginExpectation;
}
- (XCTestExpectation *)createProgressExpectationsWithTestCase:(XCTestCase *)testCase{
    self.progressExpectation = [testCase expectationWithDescription:[NSString stringWithFormat:@"%@.progressExpectation",NSStringFromClass(self.class)]];
    return self.progressExpectation;
}
- (void)willBeginRunning{
    [super willBeginRunning];
    [self.beginExpectation fulfill];
    if(self.progressExpectation){
        self.progressIncreat = YES;
        self.preProgressPercent = self.progress.mkapi_progressPercent;
    }
}
- (void)didUpdateProgress{
    [super didUpdateProgress];
    if(self.progressExpectation){
        CGFloat progressPercent = self.progress.mkapi_progressPercent;
//        NSLog(@"progress:progressExpectation:%@,progressPercent:%@,preProgressPercent:%@",self.progress.mkapi_description,@(progressPercent),@(self.preProgressPercent));
        if(progressPercent<self.preProgressPercent){
            NSLog(@"progressPercent(%.2f)<preProgressPercent(%.2f),%@",progressPercent,self.preProgressPercent,self.progress.mkapi_description);
            self.progressIncreat = NO;
        }
        if(progressPercent<0||progressPercent>1){
            NSLog(@"progressPercent(%.2f) out of [0,1]",progressPercent);
            self.progressIncreat = NO;
        }
        if((self.preProgressPercent<=0.5&&progressPercent>0.5) || (self.preProgressPercent<0.5&&progressPercent>=0.5)){
            self.progressOver50Percent = YES;
        }
        
        self.preProgressPercent = progressPercent;
    }
}
- (void)didCompleteWithError:(nullable NSError *)error{
    [super didCompleteWithError:error];
    [self.completionExpectation fulfill];
    if(self.progressExpectation){
        if(self.progressIncreat && self.progressOver50Percent){
            [self.progressExpectation fulfill];
        }else if(!self.progressOver50Percent){
            NSLog(@"progres no over 0.5");
        }
    }
}
@end
